package com.ruoyi.web.controller.ltd_web;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ltd_web.domain.LtdContent;
import com.ruoyi.ltd_web.service.ILtdContentService;
import io.netty.util.internal.ObjectUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 内容Controller
 *
 * @author ruoyi
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/system/ltd_content")
public class LtdContentController extends BaseController
{
    @Autowired
    private ILtdContentService ltdContentService;

    /**
     * 查询内容列表
     */
//    @PreAuthorize("@ss.hasPermi('system:content:list')")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(LtdContent ltdContent)
    {
        startPage();
        List<LtdContent> list = ltdContentService.selectLtdContentList(ltdContent);
        return getDataTable(list);
    }

    /**
     * 导出内容列表
     */
    @PreAuthorize("@ss.hasPermi('system:content:export')")
    @Log(title = "内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LtdContent ltdContent)
    {
        List<LtdContent> list = ltdContentService.selectLtdContentList(ltdContent);
        ExcelUtil<LtdContent> util = new ExcelUtil<LtdContent>(LtdContent.class);
        util.exportExcel(response, list, "内容数据");
    }

    /**
     * 获取内容详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:content:query')")
    @GetMapping(value = "/{contentId}")
    public AjaxResult getInfo(@PathVariable("contentId") Long contentId)
    {
        return success(ltdContentService.selectLtdContentByContentId(contentId));
    }

    // 注入 ObjectMapper 用于 JSON 解析
    private final ObjectMapper objectMapper;

    public LtdContentController(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    /**
     * 新增内容
     */
    @PreAuthorize("@ss.hasPermi('system:content:add')")
    @Log(title = "新增内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestParam("file") MultipartFile file,@RequestParam("str") String str) throws JsonProcessingException {
        LtdContent ltdContent = objectMapper.readValue(str, LtdContent.class);
        if(ltdContent.getContentType() == 1L){
            if(file.isEmpty()){
                return AjaxResult.error();
            }
            return ltdContentService.addLtdContentAndPic(ltdContent,file);
        }
        return ltdContentService.addLtdContent(ltdContent);
    }
    /**
     * 修改内容
     */
    @PreAuthorize("@ss.hasPermi('system:content:edit')")
    @Log(title = "修改内容", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestParam("file") MultipartFile file,@RequestParam("str") String str) throws JsonProcessingException {
        LtdContent ltdContent = objectMapper.readValue(str, LtdContent.class);
        if(ltdContent.getContentType() == 1L){
            if(file.isEmpty()){
                return AjaxResult.error();
            }
           return ltdContentService.editLtdContentAndPic(ltdContent,file);
        }
        return ltdContentService.editLtdContent(ltdContent);
    }

    /**
     * 删除内容
     */
    @PreAuthorize("@ss.hasPermi('system:content:remove')")
    @Log(title = "内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contentIds}")
    public AjaxResult remove(@PathVariable Long[] contentIds)
    {
        return toAjax(ltdContentService.deleteLtdContentByContentIds(contentIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:content:edit')")
    @Log(title = "内容状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody LtdContent content)
    {
        return toAjax(ltdContentService.updateLtdContent(content));
    }
}
